package ankh.graphics;

import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import ankh.Ankh;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.egl.EGLConfig;

public class Graphics implements Renderer
{
	
	protected Ankh _ankh;
	protected GLSurfaceView _glSurfaceView;
	protected GL10 _gl;
	protected Cache _cache;
	
	public GLSurfaceView getGLSurfaceView() { return _glSurfaceView; }
	public GL10 getGL() { return _gl; }
	public Cache getCache() { return _cache; }
	
	public Graphics(Ankh ankh)
	{
		_ankh = ankh;
		
		_glSurfaceView = new GLSurfaceView(_ankh.getActivity());
		_glSurfaceView.setRenderer(this);
		
		_cache = new Cache(_ankh);
	}
	
	public void onDrawFrame(GL10 gl)
	{
		_gl = gl;
		
		_gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		_gl.glMatrixMode(GL10.GL_MODELVIEW);
		_gl.glLoadIdentity();
		
		_ankh.getScene().update();
		_ankh.getScene().draw();
	}
	
	public void onSurfaceChanged(GL10 gl, int width, int height)
	{
		_gl = gl;
		
		_gl.glViewport(0, 0, width, height);
		
		_gl.glMatrixMode(GL10.GL_PROJECTION);
		_gl.glLoadIdentity();
		GLU.gluOrtho2D(_gl, 0.0f, width, height, 0.0f);
		
		_gl.glMatrixMode(GL10.GL_MODELVIEW);
		_gl.glLoadIdentity();
	}
	
	public void onSurfaceCreated(GL10 gl, EGLConfig config)
	{
		_gl = gl;
		
		_gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		
		_gl.glEnable(GL10.GL_TEXTURE_2D);
		_gl.glEnable(GL10.GL_BLEND);
		_gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		_gl.glShadeModel(GL10.GL_SMOOTH);
		_gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
	}
	
}